package zj.xinxin.web.controller.article;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zj.xinxin.api.moudle.article.api.CategoryService;
import zj.xinxin.api.moudle.article.dto.CategoryDto;
import zj.xinxin.api.moudle.article.vo.ArticleVo;
import zj.xinxin.api.moudle.article.vo.CategoryPageSearchVo;
import zj.xinxin.api.moudle.article.vo.CategoryVo;
import zj.xinxin.common.result.PageResult;
import zj.xinxin.common.result.ResponseResult;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 分类  前端控制器
 * </p>
 *
 * @author zhoujun
 * @since 2022-04-12
 */
@RestController
@Api(tags = "分类信息")
@RequestMapping("/article/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @PostMapping("/add")
    @ApiOperation(value = "保存分类信息")
    public ResponseResult<Boolean> add(@RequestBody @Valid CategoryVo category) {
        return categoryService.addByVo(category) ?
                ResponseResult.success("添加成功", true ):
                ResponseResult.fail("保存失败", false);
    }

    @GetMapping("/page")
    @ApiOperation(value = "查询分类信息")
    public ResponseResult<PageResult<CategoryDto>> page(@Valid CategoryPageSearchVo search) {
        return ResponseResult.success(categoryService.searchCategoryByPage(search));
    }

    @GetMapping("/root")
    @ApiOperation(value = "查询根分类信息")
    public ResponseResult<List<CategoryDto>> rootList() {
        List<CategoryDto> result = categoryService.listRoot();
        return ResponseResult.success(result);
    }
}

